$(function () {
    setStatusSelect();
    getTeamMember();
    // getTeamByCode();
});
let code = sessionStorage.getItem("team_code");
var team_leader;

//通过小组编号获取小组信息
function getTeamByCode() {
    let data = myAjax("/team/getTeamByCode", "get", {"code": code}, "json");
    console.log(data);
    let team = data.model["team"];
    $("#code").val(team.code);
    $("#name").val(team.name);
    // console.log(team.leader);
    team_leader = team.leader;
    console.log(team_leader);
    if (team.leader == null)
        $("#leader").append("<option value='noleader' selected='selected'>暂无组长</option>");
    else
        $("#leader").find("option[value=" + team.leader.username + "]").attr("selected", true);
    $("input[name=status][value=" + team.status.code + "]").attr("checked", true);
}

function teamEdit() {
    let name = $("#name").val();
    let leader = $("#leader").find("option:selected").val();
    team_leader = leader;
    let status = $("input[name=status]:checked").val();
    let data = {
        "name": name,
        "status": status,
        "leader": leader,
        "code": code
    }
    let result = myAjax("/team/editTeam", "post", data, "json");
    if (result == 200)
        alert("修改成功！");
    else if (result == 500)
        alert("后台错误");
    else
        alert("404未找到");
    window.location.href = "/html/home.html";
}

function getTeamMember() {
    let no_team = myAjax("/user/getUserByTeam", "get", {"team": "noteam"}, "json");
    console.log(no_team);
    $(".team").html("");
    $.each(no_team, function (i, data) {
        $("#no_team").append("<div onclick='selectedEvent($(this))'><input hidden='hidden' value='" + data.username + "'/>" + data.name + "</div>");
    })

    let in_team = myAjax("/user/getUserByTeam", "get", {"team": code}, "json");
    console.log(in_team);
    $("#leader").html("");
    $.each(in_team, function (i, data) {
        $("#in_team").append("<div onclick='selectedEvent($(this))'><input hidden='hidden' value='" + data.username + "'/>" + data.name + "</div>");
    //     if (data.team.leader == code){
    //         $("#leader").html("");
    //         $("#leader").append("<option value='" + data.username + "' selected='selected'>" + data.name + "</option>");
    // }else
            $("#leader").append("<option value='" + data.username + "'>" + data.name + "</option>");
    })
    getTeamByCode();
}

function addMember() {
    let no_team_member = new Array();
    $('#no_team>div[class=selected]>input').each(function (key, value) {
        no_team_member[key] = $(this).val();//no_team_member[key] = $(value).val();如果是其他标签 用 html();
    });
    console.log(no_team_member);
    let map = {
        "members": JSON.stringify(no_team_member),
        "teamCode": code
    }
    let data = myAjax("/user/updateUserTeam", "post", map, "json");
    if (data == 200) {
        freshTeamCrew();
        getTeamMember();
    } else if (data == 500)
        alert("后台错误");
    else
        alert("404未找到");
}

function removeMember() {
    let in_team_member = new Array();
    $('#in_team>div[class=selected]>input').each(function (key, value) {
        in_team_member[key] = $(this).val();      //如果是其他标签 用 html();
    });
    console.log(in_team_member);
    let map = {
        "members": JSON.stringify(in_team_member),
        "teamCode": "noteam"
    }
    let data = myAjax("/user/updateUserTeam", "post", map, "json");
    if (data == 200) {
        freshTeamCrew();
        getTeamMember();
    } else if (data == 500)
        alert("后台错误");
    else
        alert("404未找到");
}

function selectedEvent(obj) {
    console.log(obj[0].className);
    if (obj[0].className == 'selected')
        obj.removeClass("selected");
    else
        obj.addClass("selected");
}

function setStatusSelect() {
    if (sessionStorage.getItem("status") == null)
        getData();
    let status = JSON.parse(sessionStorage.getItem("status"));
    console.log(status);
    $("#status").html("");
    $.each(status, function (i, option) {
        $("#status").append("<label><input type='radio' name='status' value=" + option.code + ">" + option.name + "</label>");
    });
}

function freshTeamCrew() {
    myAjax("/team/freshTeamCrew", "post", {"code": code}, "json");
}